package Controller;


import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.DAOFactory;
import DAO.EmployeeDAO;
import DAO.RankDAO;
import Model.*;

/**
 * Servlet implementation class RankDelete
 */
@WebServlet("/EmployeeResetPassword")
public class EmployeeResetPassword extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EmployeeResetPassword() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException
	{
		Authenticate(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException
	{
		Authenticate(request,response);
	}
	
	protected void Authenticate (HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException
	{
		//Authenticate before starting process
		if ( LoginAdmin.CheckObject(request, response) ) {
			doProcess(request,response);
		} else {	//Otherwise Authentication Object not found
			response.sendRedirect(LoginAdmin.LOGIN_CONTROL);
		}
	}
	protected void doProcess(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException
	{
		LoginAdmin.CheckObject(request, response);
		String empId = request.getParameter("EmployeeId");
		if (empId != null) {				//If RankId found
			if ( empId.matches("[0-9]+") )	//If RankId is number
			{
				int rankInt = Integer.parseInt(empId);
				EmployeeDAO empDAO = DAOFactory.getInstance().getEmployeeDAO();
				try{
					empDAO.resetPassword(rankInt);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		response.sendRedirect("EmployeeList");	//Redirect here
	}
}
